package com.fan.play.utils

import android.app.Application
import android.content.Context
import android.content.pm.PackageManager
import androidx.core.app.ActivityCompat
import com.fan.play.manager.ActivityStack

object AppUtil {

    private const val REQUEST_CODE = 100

    private lateinit var context: Application

    fun init(app: Application) {
        context = app
    }

    fun getContext(): Context {
        return context
    }

    /**
     * 获取系统服务
     */
    fun <T> getService(serviceName: String): T {
        return context.getSystemService(serviceName) as T
    }

    /**
     * 检测权限
     * @return true：无权限
     */
    fun checkPermission(permissionName: String): Boolean {
        val activity = ActivityStack.top()
        val status = ActivityCompat.checkSelfPermission(activity, permissionName)
        if (status != PackageManager.PERMISSION_GRANTED) {
            ActivityCompat.requestPermissions(activity, arrayOf(permissionName), REQUEST_CODE)
            return false
        }
        return true
    }
}